home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / compress / unzip531.zip / win32 / win32.c < prev   
C/C++ Source or Header  |  1997-05-12  |  67KB  |  1,911 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   win32.c
  4.  
  5.   32-bit Windows-specific (NT/95) routines for use with Info-ZIP's UnZip 5.3
  6.   and later.
  7.  
  8.   Contains:  GetLoadPath()
  9.              Opendir()
  10.              Readdir()
  11.              Closedir()
  12.              process_defer_NT()   process any deferred items
  13.              SetSD()              set security descriptor on file
  14.              EvalExtraFields()    evaluate and process and extra field NOW
  15.              IsWinNT()            indicate type of WIN32 platform
  16.              test_NT()            test integrity of NT security data
  17.              utime2FileTime()
  18.              NTQueryTargetFS()
  19.              UTCtime2Localtime()
  20.              NTtzbugWorkaround()
  21.              getNTfiletime()
  22.              close_outfile()
  23.              isfloppy()
  24.              IsVolumeOldFAT()
  25.              IsFileNameValid()
  26.              do_wild()
  27.              mapattr()
  28.              mapname()
  29.              map2fat()
  30.              checkdir()
  31.              version()
  32.              stat_bandaid()       [Watcom only]
  33.              getch()              [Watcom only]
  34.  
  35.   ---------------------------------------------------------------------------*/
  36.  
  37.  
  38. #define UNZIP_INTERNAL
  39. #include "unzip.h"
  40. #include <windows.h>   /* must be AFTER unzip.h to avoid struct G problems */
  41. #ifdef __RSXNT__
  42. #  include "win32/rsxntwin.h"
  43. #endif
  44. #include "win32/nt.h"
  45.  
  46.  
  47. #if (defined(__GO32__) || defined(__EMX__))
  48. #  include <dirent.h>        /* use readdir() */
  49. #  define MKDIR(path,mode)   mkdir(path,mode)
  50. #  define Opendir  opendir
  51. #  define Readdir  readdir
  52. #  define Closedir closedir
  53. #  define zdirent  dirent
  54. #  define zDIR     DIR
  55. #else /* !(__GO32__ || __EMX__) */
  56. #  define MKDIR(path,mode)   mkdir(path)
  57.  
  58.    typedef struct zdirent {
  59.        char    reserved [21];
  60.        char    ff_attrib;
  61.        short   ff_ftime;
  62.        short   ff_fdate;
  63.        long    size;
  64.        char    d_name[MAX_PATH];
  65.        int     d_first;
  66.        HANDLE  d_hFindFile;
  67.    } zDIR;
  68.  
  69.    static zDIR           *Opendir  (const char *n);
  70.    static struct zdirent *Readdir  (zDIR *d);
  71.    static void            Closedir (zDIR *d);
  72. #endif /* ?(__GO32__ || __EMX__) */
  73.  
  74.  
  75. /* Function prototypes */
  76. #ifdef NTSD_EAS
  77.    static int  SetSD(__GPRO__ char *path, PVOLUMECAPS VolumeCaps,
  78.                      uch *eb_ptr, unsigned eb_len);
  79.    static int  EvalExtraFields(__GPRO__ char *path, uch *ef_ptr,
  80.                                unsigned ef_len);
  81. #endif
  82.  
  83. #if (defined(USE_EF_UT_TIME) || defined(NT_TZBUG_WORKAROUND))
  84.    static void utime2FileTime(time_t ut, FILETIME *pft);
  85.    static int NTQueryTargetFS(char *path);
  86. #endif
  87. #ifdef NT_TZBUG_WORKAROUND
  88.    static time_t UTCtime2Localtime(time_t utctime);
  89.    static void NTtzbugWorkaround(time_t ut, FILETIME *pft);
  90. #endif /* NT_TZBUG_WORKAROUND */
  91.  
  92. static int  getNTfiletime   (__GPRO__ FILETIME *pModFT, FILETIME *pAccFT,
  93.                              FILETIME *pCreFT);
  94. static int  isfloppy        (int nDrive);
  95. static int  IsVolumeOldFAT  (char *name);
  96. static int  IsFileNameValid (char *name);
  97. static void map2fat         (char *pathcomp, char **pEndFAT);
  98.  
  99.  
  100. /* static int created_dir;      */     /* used by mapname(), checkdir() */
  101. /* static int renamed_fullpath; */     /* ditto */
  102. /* static int fnlen;            */     /* ditto */
  103. /* static unsigned nLabelDrive; */     /* ditto */
  104.  
  105. extern char Far TruncNTSD[];    /* in extract.c */
  106.  
  107.  
  108.  
  109. #ifdef SFX
  110.  
  111. /**************************/
  112. /* Function GetLoadPath() */
  113. /**************************/
  114.  
  115. char *GetLoadPath(__GPRO)
  116. {
  117. #ifdef MSC
  118.     extern char *_pgmptr;
  119.     return _pgmptr;
  120.  
  121. #else    /* use generic API call */
  122.  
  123.     GetModuleFileName(NULL, G.filename, FILNAMSIZ-1);
  124.     _ISO_INTERN(G.filename);    /* translate to codepage of C rtl's stdio */
  125.     return G.filename;
  126. #endif
  127.  
  128. } /* end function GetLoadPath() */
  129.  
  130.  
  131.  
  132.  
  133.  
  134. #else /* !SFX */
  135.  
  136. #if (!defined(__GO32__) && !defined(__EMX__))
  137.  
  138. /**********************/        /* Borrowed from ZIP 2.0 sources            */
  139. /* Function Opendir() */        /* Difference: no special handling for      */
  140. /**********************/        /*             hidden or system files.      */
  141.  
  142. static zDIR *Opendir(n)
  143.     const char *n;          /* directory to open */
  144. {
  145.     zDIR *d;                /* malloc'd return value */
  146.     char *p;                /* malloc'd temporary string */
  147.     WIN32_FIND_DATA fd;
  148.     int len = strlen(n);
  149.  
  150.     /* Start searching for files in the MSDOS directory n */
  151.  
  152.     if ((d = (zDIR *)malloc(sizeof(zDIR))) == NULL ||
  153.         (p = malloc(strlen(n) + 5)) == NULL)
  154.     {
  155.         if (d != (zDIR *)NULL)
  156.             free((void *)d);
  157.         return (zDIR *)NULL;
  158.     }
  159.     INTERN_TO_ISO(n, p);
  160.     if (p[len-1] == ':')
  161.         p[len++] = '.';   /* x: => x:. */
  162.     else if (p[len-1] == '/' || p[len-1] == '\\')
  163.         --len;            /* foo/ => foo */
  164.     strcpy(p+len, "/*");
  165.  
  166.     if (INVALID_HANDLE_VALUE == (d->d_hFindFile = FindFirstFile(p, &fd))) {
  167.         free((zvoid *)d);
  168.         free((zvoid *)p);
  169.         return NULL;
  170.     }
  171.     strcpy(d->d_name, fd.cFileName);
  172.  
  173.     free((zvoid *)p);
  174.     d->d_first = 1;
  175.     return d;
  176.  
  177. } /* end of function Opendir() */
  178.  
  179.  
  180.  
  181.  
  182. /**********************/        /* Borrowed from ZIP 2.0 sources            */
  183. /* Function Readdir() */        /* Difference: no special handling for      */
  184. /**********************/        /*             hidden or system files.      */
  185.  
  186. static struct zdirent *Readdir(d)
  187.     zDIR *d;                    /* directory stream from which to read */
  188. {
  189.     /* Return pointer to first or next directory entry, or NULL if end. */
  190.  
  191.     if ( d->d_first )
  192.         d->d_first = 0;
  193.     else
  194.     {
  195.         WIN32_FIND_DATA fd;
  196.  
  197.         if ( !FindNextFile(d->d_hFindFile, &fd) )
  198.             return NULL;
  199.  
  200.         ISO_TO_INTERN(fd.cFileName, d->d_name);
  201.     }
  202.     return (struct zdirent *)d;
  203.  
  204. } /* end of function Readdir() */
  205.  
  206.  
  207.  
  208.  
  209. /***********************/
  210. /* Function Closedir() */       /* Borrowed from ZIP 2.0 sources */
  211. /***********************/
  212.  
  213. static void Closedir(d)
  214.     zDIR *d;                    /* directory stream to close */
  215. {
  216.     FindClose(d->d_hFindFile);
  217.     free(d);
  218. }
  219.  
  220. #endif /* !__GO32__ && !__EMX__ */
  221. #endif /* ?SFX */
  222.  
  223.  
  224.  
  225.  
  226. #ifdef NTSD_EAS
  227.  
  228. /*********************************/
  229. /*  Function process_defer_NT()  */
  230. /*********************************/
  231.  
  232. void process_defer_NT(__G)
  233.     __GDEF
  234. {
  235.     /* process deferred items */
  236.  
  237.     unsigned long dir, bytes;
  238.     unsigned long dirfail, bytesfail;
  239.  
  240. #ifndef NO_NTSD_WITH_RSXNT
  241.     ProcessDefer(&dir, &bytes, &dirfail, &bytesfail);
  242. #else
  243.     dir = bytes = dirfail = bytesfail = 0L;
  244. #endif
  245.  
  246.     if (!G.tflag && (G.qflag < 2)) {
  247.         if (dir)
  248.             Info(slide, 0, ((char *)slide,
  249.               "    updated: %lu directory entries with %lu bytes security",
  250.               dir, bytes));
  251.         if (dirfail)
  252.             Info(slide, 0, ((char *)slide,
  253.               "     failed: %lu directory entries with %lu bytes security",
  254.               dirfail, bytesfail));
  255.     }
  256. }
  257.  
  258.  
  259.  
  260. /**********************/
  261. /*  Function SetSD()  */   /* return almost-PK errors */
  262. /**********************/
  263.  
  264. static int SetSD(__G__ path, VolumeCaps, eb_ptr, eb_len)
  265.     __GDEF
  266.     char *path;
  267.     PVOLUMECAPS VolumeCaps;
  268.     uch *eb_ptr;
  269.     unsigned eb_len;
  270. {
  271.     ulg ntsd_ucSize = makelong(eb_ptr + (EB_HEADSIZE+EB_NTSD_LSIZE));
  272.     uch ntsd_Version = *(eb_ptr + (EB_HEADSIZE+EB_NTSD_VERSION));
  273.     uch *security_data;
  274.     int error;
  275.  
  276.     if (eb_ptr == NULL || eb_len < EB_NTSD_L_LEN)
  277.         return PK_OK;  /* not a valid NTSD extra field:  assume OK */
  278.  
  279.     /* check if we know how to handle this version */
  280.     if (ntsd_Version > EB_NTSD_MAX_VER_SUPPORT)
  281.         return PK_OK;
  282.  
  283.     if (ntsd_ucSize > 0L && eb_len <= (EB_NTSD_L_LEN + 6))
  284.         return IZ_EF_TRUNC;               /* no compressed data! */
  285.  
  286.     /* allocate storage for uncompressed data */
  287.     security_data = (uch *)malloc((extent)ntsd_ucSize);
  288.     if (security_data == NULL)
  289.         return PK_MEM4;
  290.  
  291.     error = memextract(__G__ security_data, ntsd_ucSize,
  292.       (eb_ptr + (EB_HEADSIZE+EB_NTSD_L_LEN)), (ulg)(eb_len - EB_NTSD_L_LEN));
  293.  
  294.     if (error == PK_OK) {
  295. #ifdef NO_NTSD_WITH_RSXNT
  296.         Info(slide, 0, ((char *)slide,
  297.           "%s port does not yet support setting security", "RSXNT"));
  298.         /* error = PK_OK; */  /* GRR:  change to PK_WARN? */
  299. #else /* !NO_NTSD_WITH_RSXNT */
  300.         if (SecuritySet(path, VolumeCaps, security_data)) {
  301.             error = PK_COOL;
  302.             if (!G.tflag && (G.qflag < 2) &&
  303.                 (!(VolumeCaps->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)))
  304.                 Info(slide, 0, ((char *)slide, " (%ld bytes security)",
  305.                   ntsd_ucSize));
  306.         }
  307. #endif /* ?NO_NTSD_WITH_RSXNT */
  308.     }
  309.  
  310.     free(security_data);
  311.     return error;
  312. }
  313.  
  314.  
  315.  
  316.  
  317. /********************************/   /* scan extra fields for something */
  318. /*  Function EvalExtraFields()  */   /*  we happen to know */
  319. /********************************/
  320.  
  321. static int EvalExtraFields(__G__ path, ef_ptr, ef_len)
  322.     __GDEF
  323.     char *path;
  324.     uch *ef_ptr;
  325.     unsigned ef_len;
  326. {
  327.     int rc = PK_OK;
  328.  
  329.     if (!G.X_flag)
  330.         return PK_OK;  /* user said don't process ACLs; for now, no other
  331.                           extra block types are handled here */
  332.  
  333.     while (ef_len >= EB_HEADSIZE)
  334.     {
  335.         unsigned eb_id = makeword(EB_ID + ef_ptr);
  336.         unsigned eb_len = makeword(EB_LEN + ef_ptr);
  337.  
  338.         if (eb_len > (ef_len - EB_HEADSIZE)) {
  339.             /* discovered some extra field inconsistency! */
  340.             Trace((stderr,
  341.               "EvalExtraFields: block length %u > rest ef_size %u\n", eb_len,
  342.               ef_len - EB_HEADSIZE));
  343.             break;
  344.         }
  345.  
  346.         switch (eb_id)
  347.         {
  348.             /* process security descriptor extra data if:
  349.                  Caller is WinNT AND
  350.                  Target local/remote drive supports acls AND
  351.                  Target file is not a directory (else we defer processing
  352.                    until later)
  353.              */
  354.             case EF_NTSD:
  355.                 if (IsWinNT()) {
  356.                     VOLUMECAPS VolumeCaps;
  357.  
  358.                     /* provide useful input */
  359.                     VolumeCaps.dwFileAttributes = G.pInfo->file_attr;
  360.                     VolumeCaps.bUsePrivileges = (G.X_flag > 1);
  361.  
  362. #ifndef NO_NTSD_WITH_RSXNT
  363.                     /* check target volume capabilities - just fall through
  364.                      * and try if fail */
  365.                     if (GetVolumeCaps(G.rootpath, path, &VolumeCaps) &&
  366.                         !(VolumeCaps.dwFileSystemFlags & FS_PERSISTENT_ACLS))
  367.                     {
  368.                         rc = PK_OK;
  369.                         break;
  370.                     }
  371. #endif
  372.                     rc = SetSD(__G__ path, &VolumeCaps, ef_ptr, eb_len);
  373.                 } else
  374.                     rc = PK_OK;
  375.                 break;
  376.  
  377. #if 0
  378.             /* perhaps later we can add support for unzipping OS/2 EAs to NT */
  379.             case EF_OS2:
  380.                 rc = SetEAs(__G__ path, ef_ptr);
  381.                 break;
  382.  
  383.             case EF_IZUNIX:
  384.             case EF_IZUNIX2:
  385.             case EF_TIME:
  386.                 break;          /* handled elsewhere */
  387. #else /* ! 0 */
  388. #ifdef DEBUG
  389.             case EF_AV:
  390.             case EF_OS2:
  391.             case EF_PKVMS:
  392.             case EF_PKUNIX:
  393.             case EF_IZVMS:
  394.             case EF_IZUNIX:
  395.             case EF_IZUNIX2:
  396.             case EF_TIME:
  397.             case EF_JLMAC:
  398.             case EF_ZIPIT:
  399.             case EF_VMCMS:
  400.             case EF_MVS:
  401.             case EF_ACL:
  402.             case EF_BEOS:
  403.             case EF_QDOS:
  404.             case EF_AOSVS:
  405.             case EF_SPARK:
  406.             case EF_MD5:
  407.             case EF_ASIUNIX:
  408.                 break;          /* shut up for other known e.f. blocks  */
  409. #endif /* DEBUG */
  410. #endif /* ? 0 */
  411.  
  412.             default:
  413.                 Trace((stderr,
  414.                   "EvalExtraFields: unknown extra field block, ID=%u\n",
  415.                   eb_id));
  416.                 break;
  417.         }
  418.  
  419.         ef_ptr += (eb_len + EB_HEADSIZE);
  420.         ef_len -= (eb_len + EB_HEADSIZE);
  421.  
  422.         if (rc != PK_OK)
  423.             break;
  424.     }
  425.  
  426.     return rc;
  427. }
  428.  
  429. #endif /* NTSD_EAS */
  430.  
  431.  
  432.  
  433.  
  434. /**********************/
  435. /* Function IsWinNT() */
  436. /**********************/
  437.  
  438. int IsWinNT(void)       /* returns TRUE if real NT, FALSE if Win95 or Win32s */
  439. {
  440.     static DWORD g_PlatformId = 0xFFFFFFFF; /* saved platform indicator */
  441.  
  442.     if (g_PlatformId == 0xFFFFFFFF) {
  443.         /* note: GetVersionEx() doesn't exist on WinNT 3.1 */
  444.         if (GetVersion() < 0x80000000)
  445.             g_PlatformId = TRUE;
  446.         else
  447.             g_PlatformId = FALSE;
  448.     }
  449.     return (int)g_PlatformId;
  450. }
  451.  
  452.  
  453.  
  454.  
  455. #ifndef SFX
  456.  
  457. /************************/   /* can return PK_OK, PK_ERR, PK_MEM3, PK_MEM4, */
  458. /*  Function test_NT()  */   /*  IZ_EF_TRUNC, or (if testing) PK_ERR or'd */
  459. /************************/   /*  with compression method in high byte */
  460.  
  461. int test_NT(__G__ eb, eb_size)
  462.     __GDEF
  463.     uch *eb;
  464.     unsigned eb_size;
  465. {
  466.     ulg eb_ucsize = makelong(eb + (EB_HEADSIZE+EB_NTSD_LSIZE));
  467.     uch *eb_uncompressed;
  468.     int r;
  469.  
  470.     if (eb_ucsize > 0L && eb_size <= (EB_NTSD_L_LEN + 6))
  471.         return IZ_EF_TRUNC;             /* no compressed data! */
  472.  
  473.     if ((eb_uncompressed = (uch *)malloc((extent)eb_ucsize)) == (uch *)NULL)
  474.         return PK_MEM4;
  475.  
  476.     r = memextract(__G__ eb_uncompressed, eb_ucsize, (eb + EB_NTSD_L_LEN),
  477.                    (ulg)(eb_size - (EB_NTSD_L_LEN - EB_HEADSIZE)));
  478.  
  479. #ifndef NO_NTSD_WITH_RSXNT
  480.     if (r == PK_OK  &&  !ValidateSecurity(eb_uncompressed))
  481.         r = PK_ERR;
  482. #endif
  483.  
  484.     free(eb_uncompressed);
  485.     return r;
  486.  
  487. } /* end function test_NT() */
  488.  
  489. #endif /* !SFX */
  490.  
  491.  
  492.  
  493.  
  494. #if (defined(USE_EF_UT_TIME) || defined(NT_TZBUG_WORKAROUND))
  495.  
  496. /*****************************/
  497. /* Function utime2FileTime() */     /* convert Unix time_t format into the */
  498. /*****************************/     /* form used by SetFileTime() in NT/95 */
  499.  
  500. #define UNIX_TIME_ZERO_HI  0x019DB1DE
  501. #define UNIX_TIME_ZERO_LO  0xD53E8000
  502. #define NT_QUANTA_PER_UNIX 10000000
  503.  
  504. static void utime2FileTime(time_t ut, FILETIME *pft)
  505. {
  506. #if defined(__GNUC__) || defined(ULONG_LONG_MAX)
  507.     unsigned long long NTtime;
  508.  
  509.     NTtime = ((unsigned long long)ut * NT_QUANTA_PER_UNIX) +
  510.              ((unsigned long long)UNIX_TIME_ZERO_LO +
  511.               ((unsigned long long)UNIX_TIME_ZERO_HI << 32));
  512.     pft->dwLowDateTime = (DWORD)NTtime;
  513.     pft->dwHighDateTime = (DWORD)(NTtime >> 32);
  514.  
  515. #else /* "unsigned long long" may not be supported */
  516.     unsigned int b1, b2, carry = 0;
  517.     unsigned long r0, r1, r2, r3, r4;
  518.  
  519.     b1 = ut & 0xFFFF;
  520.     b2 = (ut >> 16) & 0xFFFF;       /* if ut is over 32 bits, too bad */
  521.     r1 = b1 * (NT_QUANTA_PER_UNIX & 0xFFFF);
  522.     r2 = b1 * (NT_QUANTA_PER_UNIX >> 16);
  523.     r3 = b2 * (NT_QUANTA_PER_UNIX & 0xFFFF);
  524.     r4 = b2 * (NT_QUANTA_PER_UNIX >> 16);
  525.     r0 = (r1 + (r2 << 16)) & 0xFFFFFFFF;
  526.     if (r0 < r1)
  527.         carry++;
  528.     r1 = r0;
  529.     r0 = (r0 + (r3 << 16)) & 0xFFFFFFFF;
  530.     if (r0 < r1)
  531.         carry++;
  532.     pft->dwLowDateTime = r0 + UNIX_TIME_ZERO_LO;
  533.     if (pft->dwLowDateTime < r0)
  534.         carry++;
  535.     pft->dwHighDateTime = r4 + (r2 >> 16) + (r3 >> 16)
  536.                             + UNIX_TIME_ZERO_HI + carry;
  537. #endif /* ?(64-bit "unsigned long long" support) */
  538.  
  539. } /* end function utime2FileTime() */
  540.  
  541.  
  542.  
  543. /******************************/
  544. /* Function NTQueryTargetFS() */
  545. /******************************/
  546.  
  547. static int NTQueryTargetFS(char *path)
  548. {
  549.     char     *tmp0;
  550.     char      rootPathName[4];
  551.     char      tmp1[MAX_PATH], tmp2[MAX_PATH];
  552.     unsigned  volSerNo, maxCompLen, fileSysFlags;
  553. #ifdef __RSXNT__        /* RSXNT/EMX C rtl uses OEM charset */
  554.     char *ansi_path = (char *)alloca(strlen(path) + 1);
  555.  
  556.     INTERN_TO_ISO(path, ansi_path);
  557.     path = ansi_path;
  558. #endif
  559.  
  560.     if (isalpha(path[0]) && (path[1] == ':'))
  561.         tmp0 = path;
  562.     else
  563.     {
  564.         GetFullPathName(path, MAX_PATH, tmp1, &tmp0);
  565.         tmp0 = &tmp1[0];
  566.     }
  567.     strncpy(rootPathName, tmp0, 3);   /* Build the root path name, */
  568.     rootPathName[3] = '\0';           /* e.g. "A:/"                */
  569.  
  570.     GetVolumeInformation((LPCTSTR)rootPathName, (LPTSTR)tmp1, (DWORD)MAX_PATH,
  571.                          (LPDWORD)&volSerNo, (LPDWORD)&maxCompLen,
  572.                          (LPDWORD)&fileSysFlags, (LPTSTR)tmp2, (DWORD)MAX_PATH);
  573.  
  574.     /* Volumes in (V)FAT and (OS/2) HPFS format store file timestamps in
  575.      * local time!
  576.      */
  577.     return !strncmp(strupr(tmp2), "FAT", 3) ||
  578.            !strncmp(tmp2, "VFAT", 4) ||
  579.            !strncmp(tmp2, "HPFS", 4);
  580.  
  581. } /* end function NTQueryTargetFS() */
  582.  
  583. #endif /* USE_EF_UT_TIME || NT_TZBUG_WORKAROUND */
  584.  
  585.  
  586.  
  587. #ifndef NT_TZBUG_WORKAROUND
  588. #  define UTIME_BOUNDCHECK_1(utimval) \
  589.      if (fs_uses_loctime) { \
  590.          utime_dosmin = dos_to_unix_time(0x0021, 0); \
  591.          if ((ulg)utimval < (ulg)utime_dosmin) \
  592.              utimval = utime_dosmin; \
  593.      }
  594. #  define UTIME_BOUNDCHECK_N(utimval) \
  595.      if (fs_uses_loctime && ((ulg)utimval < (ulg)utime_dosmin)) \
  596.          utimval = utime_dosmin;
  597. #  define NT_TZBUG_PRECOMPENSATE(ut, pft)
  598.  
  599. #else
  600. #  define UTIME_1980_JAN_01_00_00   315532800L
  601. #  define UTIME_BOUNDCHECK_1(utimval)
  602. #  define UTIME_BOUNDCHECK_N(utimval)
  603. #  define NT_TZBUG_PRECOMPENSATE(ut, pft) \
  604.      if (fs_uses_loctime) NTtzbugWorkaround(ut, pft);
  605.  
  606.    /* nonzero if `y' is a leap year, else zero */
  607. #  define leap(y) (((y)%4 == 0 && (y)%100 != 0) || (y)%400 == 0)
  608.    /* number of leap years from 1970 to `y' (not including `y' itself) */
  609. #  define nleap(y) (((y)-1969)/4 - ((y)-1901)/100 + ((y)-1601)/400)
  610.  
  611. extern ZCONST ush ydays[];
  612.  
  613. /********************************/
  614. /* Function UTCtime2Localtime() */   /* borrowed from Zip's mkgmtime() */
  615. /********************************/
  616.  
  617. static time_t UTCtime2Localtime(time_t utctime)
  618. {
  619.     time_t utc = utctime;
  620.     struct tm *tm;
  621.     unsigned years, months, days, hours, minutes, seconds;
  622.  
  623.  
  624. #ifdef __BORLANDC__   /* Borland C++ 5.x crashes when trying to reference tm */
  625.     if (utc < UTIME_1980_JAN_01_00_00)
  626.         utc = UTIME_1980_JAN_01_00_00;
  627. #endif
  628.     tm = localtime(&utc);
  629.  
  630.     years = tm->tm_year + 1900;  /* year - 1900 -> year */
  631.     months = tm->tm_mon;         /* 0..11 */
  632.     days = tm->tm_mday - 1;      /* 1..31 -> 0..30 */
  633.     hours = tm->tm_hour;         /* 0..23 */
  634.     minutes = tm->tm_min;        /* 0..59 */
  635.     seconds = tm->tm_sec;        /* 0..61 in ANSI C */
  636.  
  637.     /* set `days' to the number of days into the year */
  638.     days += ydays[months] + (months > 1 && leap(years));
  639.  
  640.     /* now set `days' to the number of days since 1 Jan 1970 */
  641.     days += 365 * (years - 1970) + nleap(years);
  642.  
  643.     return (time_t)(86400L * (ulg)days + 3600L * (ulg)hours +
  644.                     (ulg)(60 * minutes + seconds));
  645.  
  646. } /* end function UTCtime2Localtime() */
  647.  
  648.  
  649.  
  650. /********************************/
  651. /* Function NTtzbugWorkaround() */
  652. /********************************/
  653.  
  654. static void NTtzbugWorkaround(time_t ut, FILETIME *pft)
  655. {
  656.     FILETIME C_RTL_locft, NTAPI_locft;
  657.     time_t ux_loctime = UTCtime2Localtime(ut);
  658.  
  659.     /* This routine is only used when the target file system stores time-
  660.      * stamps as local time in MSDOS format.  Thus we make sure that the
  661.      * resulting timestamp is within the range of MSDOS date-time values. */
  662.     if (ux_loctime < UTIME_1980_JAN_01_00_00)
  663.         ux_loctime = UTIME_1980_JAN_01_00_00;
  664.  
  665.     utime2FileTime(ux_loctime, &C_RTL_locft);
  666.     if (!FileTimeToLocalFileTime(pft, &NTAPI_locft))
  667.         return;
  668.     else {
  669.         long time_shift_l, time_shift_h;
  670.         int carry = 0;
  671.  
  672.         time_shift_l = C_RTL_locft.dwLowDateTime - NTAPI_locft.dwLowDateTime;
  673.         if (C_RTL_locft.dwLowDateTime < NTAPI_locft.dwLowDateTime)
  674.             carry--;
  675.         time_shift_h = C_RTL_locft.dwHighDateTime - NTAPI_locft.dwHighDateTime;
  676.         pft->dwLowDateTime += time_shift_l;
  677.         if (pft->dwLowDateTime < (ulg)time_shift_l)
  678.             carry++;
  679.         pft->dwHighDateTime += time_shift_h + carry;
  680.     }
  681. } /* end function NTtzbugWorkaround() */
  682.  
  683. #endif /* ?NT_TZBUG_WORKAROUND */
  684.  
  685.  
  686.  
  687. /****************************/      /* Get the file time in a format that */
  688. /* Function getNTfiletime() */      /*  can be used by SetFileTime() in NT */
  689. /****************************/
  690.  
  691. static int getNTfiletime(__G__ pModFT, pAccFT, pCreFT)
  692.     __GDEF
  693.     FILETIME *pModFT;
  694.     FILETIME *pAccFT;
  695.     FILETIME *pCreFT;
  696. {
  697. #ifdef NT_TZBUG_WORKAROUND
  698.     time_t ux_modtime;
  699. #else /* !NT_TZBUG_WORKAROUND */
  700.     FILETIME locft;    /* 64-bit value made up of two 32-bit [low & high] */
  701.     WORD wDOSDate;     /* for converting from DOS date to Windows NT */
  702.     WORD wDOSTime;
  703. #endif /* ?NT_TZBUG_WORKAROUND */
  704. #ifdef USE_EF_UT_TIME
  705.     unsigned eb_izux_flg;
  706.     iztimes z_utime;   /* struct for Unix-style actime & modtime, + creatime */
  707. #endif
  708. #if (defined(USE_EF_UT_TIME) && !defined(NT_TZBUG_WORKAROUND))
  709.     time_t utime_dosmin;
  710. # endif
  711. #if (defined(USE_EF_UT_TIME) || defined(NT_TZBUG_WORKAROUND))
  712.     int fs_uses_loctime = NTQueryTargetFS(G.filename);
  713. #endif
  714.  
  715.     /* Copy and/or convert time and date variables, if necessary;
  716.      * return a flag indicating which time stamps are available. */
  717. #ifdef USE_EF_UT_TIME
  718.     if (G.extra_field && ((eb_izux_flg = ef_scan_for_izux(G.extra_field,
  719.         G.lrec.extra_field_length, 0, &z_utime, NULL)) & EB_UT_FL_MTIME))
  720.     {
  721.         TTrace((stderr, "getNTfiletime:  Unix e.f. modif. time = %lu\n",
  722.           z_utime.mtime));
  723.         UTIME_BOUNDCHECK_1(z_utime.mtime)
  724.         utime2FileTime(z_utime.mtime, pModFT);
  725.         NT_TZBUG_PRECOMPENSATE(z_utime.mtime, pModFT)
  726.         if (eb_izux_flg & EB_UT_FL_ATIME) {
  727.             UTIME_BOUNDCHECK_N(z_utime.atime)
  728.             utime2FileTime(z_utime.atime, pAccFT);
  729.             NT_TZBUG_PRECOMPENSATE(z_utime.atime, pAccFT)
  730.         }
  731.         if (eb_izux_flg & EB_UT_FL_CTIME) {
  732.             UTIME_BOUNDCHECK_N(z_utime.ctime)
  733.             utime2FileTime(z_utime.ctime, pCreFT);
  734.             NT_TZBUG_PRECOMPENSATE(z_utime.ctime, pCreFT)
  735.         }
  736.         return (int)eb_izux_flg;
  737.     }
  738. #endif /* USE_EF_UT_TIME */
  739. #ifdef NT_TZBUG_WORKAROUND
  740.     ux_modtime = dos_to_unix_time(G.lrec.last_mod_file_date,
  741.                                   G.lrec.last_mod_file_time);
  742.     utime2FileTime(ux_modtime, pModFT);
  743.     NT_TZBUG_PRECOMPENSATE(ux_modtime, pModFT)
  744. #else /* !NT_TZBUG_WORKAROUND */
  745.  
  746.     wDOSTime = (WORD)G.lrec.last_mod_file_time;
  747.     wDOSDate = (WORD)G.lrec.last_mod_file_date;
  748.  
  749.     /* The DosDateTimeToFileTime() function converts a DOS date/time
  750.      * into a 64-bit Windows NT file time */
  751.     if (!DosDateTimeToFileTime(wDOSDate, wDOSTime, &locft))
  752.     {
  753.         Info(slide, 0, ((char *)slide, "DosDateTime failed: %d\n",
  754.           (int)GetLastError()));
  755.         return 0;
  756.     }
  757.     if (!LocalFileTimeToFileTime(&locft, pModFT))
  758.     {
  759.         Info(slide, 0, ((char *)slide, "LocalFileTime failed: %d\n",
  760.           (int)GetLastError()));
  761.         *pModFT = locft;
  762.     }
  763. #endif /* ?NT_TZBUG_WORKAROUND */
  764.     *pAccFT = *pModFT;
  765.     return (EB_UT_FL_MTIME | EB_UT_FL_ATIME);
  766.  
  767. } /* end function getNTfiletime() */
  768.  
  769.  
  770.  
  771.  
  772. /****************************/
  773. /* Function close_outfile() */
  774. /****************************/
  775.  
  776. void close_outfile(__G)
  777.     __GDEF
  778. {
  779.     FILETIME Modft;    /* File time type defined in NT, `last modified' time */
  780.     FILETIME Accft;    /* NT file time type, `last access' time */
  781.     FILETIME Creft;    /* NT file time type, `file creation' time */
  782.     HANDLE hFile;      /* File handle defined in NT    */
  783.     int gotTime;
  784. #ifdef __RSXNT__        /* RSXNT/EMX C rtl uses OEM charset */
  785.     char *ansi_name = (char *)alloca(strlen(G.filename) + 1);
  786.  
  787.     INTERN_TO_ISO(G.filename, ansi_name);
  788. #   define Ansi_Fname  ansi_name
  789. #else
  790. #   define Ansi_Fname  G.filename
  791. #endif
  792.  
  793.     /* don't set the time stamp on standard output */
  794.     if (G.cflag) {
  795.         fclose(G.outfile);
  796.         return;
  797.     }
  798.  
  799.     gotTime = getNTfiletime(__G__ &Modft, &Accft, &Creft);
  800.  
  801.     /* Close the file and then re-open it using the Win32
  802.      * CreateFile call, so that the file can be created
  803.      * with GENERIC_WRITE access, otherwise the SetFileTime
  804.      * call will fail. */
  805.     fclose(G.outfile);
  806.  
  807.     /* open a handle to the file before processing extra fields;
  808.        we do this in case new security on file prevents us from updating
  809.        time stamps */
  810.     hFile = CreateFile(Ansi_Fname, GENERIC_WRITE, FILE_SHARE_WRITE, NULL,
  811.          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  812.  
  813.     /* sfield@microsoft.com: set attributes before time in case we decide to
  814.        support other filetime members later.  This also allows us to apply
  815.        attributes before the security is changed, which may prevent this
  816.        from succeeding otherwise.  Also, since most files don't have
  817.        any interesting attributes, only change them if something other than
  818.        FILE_ATTRIBUTE_ARCHIVE appears in the attributes.  This works well
  819.        as an optimization because FILE_ATTRIBUTE_ARCHIVE gets applied to the
  820.        file anyway, when it's created new. */
  821.     if((G.pInfo->file_attr & 0x7F) & ~FILE_ATTRIBUTE_ARCHIVE) {
  822.         if (!SetFileAttributes(Ansi_Fname, G.pInfo->file_attr & 0x7F))
  823.             Info(slide, 1, ((char *)slide,
  824.               "\nwarning (%d): could not set file attributes\n",
  825.               (int)GetLastError()));
  826.     }
  827.  
  828. #ifdef NTSD_EAS
  829.     /* set extra fields, both stored-in-zipfile and .LONGNAME flavors */
  830.     if (G.extra_field) {    /* zipfile extra field may have extended attribs */
  831.         int err = EvalExtraFields(__G__ G.filename, G.extra_field,
  832.                                   G.lrec.extra_field_length);
  833.  
  834.         if (err == IZ_EF_TRUNC) {
  835.             if (G.qflag)
  836.                 Info(slide, 1, ((char *)slide, "%-22s ",
  837.                   FnFilter1(G.filename)));
  838.             Info(slide, 1, ((char *)slide, LoadFarString(TruncNTSD),
  839.               makeword(G.extra_field+2)-10, G.qflag? "\n":""));
  840.         }
  841.     }
  842. #endif /* NTSD_EAS */
  843.  
  844.     if ( hFile == INVALID_HANDLE_VALUE )
  845.         Info(slide, 1, ((char *)slide,
  846.           "\nCreateFile error %d when trying set file time\n",
  847.           (int)GetLastError()));
  848.     else {
  849.         if (gotTime) {
  850.             FILETIME *pModft = (gotTime & EB_UT_FL_MTIME) ? &Modft : NULL;
  851.             FILETIME *pAccft = (gotTime & EB_UT_FL_ATIME) ? &Accft : NULL;
  852.             FILETIME *pCreft = (gotTime & EB_UT_FL_CTIME) ? &Creft : NULL;
  853.  
  854.             if (!SetFileTime(hFile, pCreft, pAccft, pModft))
  855.                 Info(slide, 0, ((char *)slide, "\nSetFileTime failed: %d\n",
  856.                   (int)GetLastError()));
  857.         }
  858.         CloseHandle(hFile);
  859.     }
  860.  
  861.     return;
  862.  
  863. #undef Ansi_Fname
  864.  
  865. } /* end function close_outfile() */
  866.  
  867.  
  868.  
  869.  
  870.  
  871. /***********************/
  872. /* Function isfloppy() */   /* more precisely, is it removable? */
  873. /***********************/
  874.  
  875. static int isfloppy(int nDrive)   /* 1 == A:, 2 == B:, etc. */
  876. {
  877.     char rootPathName[4];
  878.  
  879.     rootPathName[0] = (char)('A' + nDrive - 1);   /* build the root path */
  880.     rootPathName[1] = ':';                        /*  name, e.g. "A:/" */
  881.     rootPathName[2] = '/';
  882.     rootPathName[3] = '\0';
  883.  
  884.     return (GetDriveType(rootPathName) == DRIVE_REMOVABLE);
  885.  
  886. } /* end function isfloppy() */
  887.  
  888.  
  889.  
  890.  
  891. /*****************************/
  892. /* Function IsVolumeOldFAT() */
  893. /*****************************/
  894.  
  895. /*
  896.  * Note:  8.3 limits on filenames apply only to old-style FAT filesystems.
  897.  *        More recent versions of Windows (Windows NT 3.5 / Windows 4.0)
  898.  *        can support long filenames (LFN) on FAT filesystems.  Check the
  899.  *        filesystem maximum component length field to detect LFN support.
  900.  *        [GRR:  this routine is only used to determine whether spaces in
  901.  *        filenames are supported...]
  902.  */
  903.  
  904. static int IsVolumeOldFAT(char *name)
  905. {
  906.     char     *tmp0;
  907.     char      rootPathName[4];
  908.     char      tmp1[MAX_PATH], tmp2[MAX_PATH];
  909.     unsigned  volSerNo, maxCompLen, fileSysFlags;
  910. #ifdef __RSXNT__        /* RSXNT/EMX C rtl uses OEM charset */
  911.     char *ansi_name = (char *)alloca(strlen(name) + 1);
  912.  
  913.     INTERN_TO_ISO(name, ansi_name);
  914.     name = ansi_name;
  915. #endif
  916.  
  917.     if (isalpha(name[0]) && (name[1] == ':'))
  918.         tmp0 = name;
  919.     else
  920.     {
  921.         GetFullPathName(name, MAX_PATH, tmp1, &tmp0);
  922.         tmp0 = &tmp1[0];
  923.     }
  924.     strncpy(rootPathName, tmp0, 3);   /* Build the root path name, */
  925.     rootPathName[3] = '\0';           /* e.g. "A:/"                */
  926.  
  927.     GetVolumeInformation((LPCTSTR)rootPathName, (LPTSTR)tmp1, (DWORD)MAX_PATH,
  928.                          (LPDWORD)&volSerNo, (LPDWORD)&maxCompLen,
  929.                          (LPDWORD)&fileSysFlags, (LPTSTR)tmp2, (DWORD)MAX_PATH);
  930.  
  931.     /* Long Filenames (LFNs) are available if the component length is > 12 */
  932.     return maxCompLen <= 12;
  933. /*  return !strncmp(strupr(tmp2), "FAT", 3);   old version */
  934.  
  935. }
  936.  
  937.  
  938.  
  939.  
  940. /******************************/
  941. /* Function IsFileNameValid() */
  942. /******************************/
  943.  
  944. static int IsFileNameValid(char *name)
  945. {
  946.     HFILE    hf;
  947.     OFSTRUCT of;
  948. #ifdef __RSXNT__        /* RSXNT/EMX C rtl uses OEM charset */
  949.     char *ansi_name = (char *)alloca(strlen(name) + 1);
  950.  
  951.     INTERN_TO_ISO(name, ansi_name);
  952.     name = ansi_name;
  953. #endif
  954.  
  955.     hf = OpenFile(name, &of, OF_READ | OF_SHARE_DENY_NONE);
  956.     if (hf == HFILE_ERROR)
  957.         switch (GetLastError())
  958.         {
  959.             case ERROR_INVALID_NAME:
  960.             case ERROR_FILENAME_EXCED_RANGE:
  961.                 return FALSE;
  962.             default:
  963.                 return TRUE;
  964.         }
  965.     else
  966.         _lclose(hf);
  967.     return TRUE;
  968. }
  969.  
  970.  
  971.  
  972.  
  973. #ifndef SFX
  974.  
  975. /************************/
  976. /*  Function do_wild()  */   /* identical to OS/2 version */
  977. /************************/
  978.  
  979. char *do_wild(__G__ wildspec)
  980.     __GDEF
  981.     char *wildspec;         /* only used first time on a given dir */
  982. {
  983.  /* static zDIR *dir = NULL;                               */
  984.  /* static char *dirname, *wildname, matchname[FILNAMSIZ]; */
  985.  /* static int firstcall=TRUE, have_dirname, dirnamelen;   */
  986.     struct zdirent *file;
  987.  
  988.     /* Even when we're just returning wildspec, we *always* do so in
  989.      * matchname[]--calling routine is allowed to append four characters
  990.      * to the returned string, and wildspec may be a pointer to argv[].
  991.      */
  992.     if (!G.notfirstcall) {  /* first call:  must initialize everything */
  993.         G.notfirstcall = TRUE;
  994.  
  995.         if (!iswild(wildspec)) {
  996.             strcpy(G.matchname, wildspec);
  997.             G.have_dirname = FALSE;
  998.             G.wild_dir = NULL;
  999.             return G.matchname;
  1000.         }
  1001.  
  1002.         /* break the wildspec into a directory part and a wildcard filename */
  1003.         if ((G.wildname = strrchr(wildspec, '/')) == NULL &&
  1004.             (G.wildname = strrchr(wildspec, ':')) == NULL) {
  1005.             G.dirname = ".";
  1006.             G.dirnamelen = 1;
  1007.             G.have_dirname = FALSE;
  1008.             G.wildname = wildspec;
  1009.         } else {
  1010.             ++G.wildname;     /* point at character after '/' or ':' */
  1011.             G.dirnamelen = G.wildname - wildspec;
  1012.             if ((G.dirname = (char *)malloc(G.dirnamelen+1)) == NULL) {
  1013.                 Info(slide, 1, ((char *)slide,
  1014.                   "warning:  can't allocate wildcard buffers\n"));
  1015.                 strcpy(G.matchname, wildspec);
  1016.                 return G.matchname; /* but maybe filespec was not a wildcard */
  1017.             }
  1018.             strncpy(G.dirname, wildspec, G.dirnamelen);
  1019.             G.dirname[G.dirnamelen] = '\0';    /* terminate for strcpy below */
  1020.             G.have_dirname = TRUE;
  1021.         }
  1022.         Trace((stderr, "do_wild:  dirname = [%s]\n", G.dirname));
  1023.  
  1024.         if ((G.wild_dir = (zvoid *)Opendir(G.dirname)) != NULL) {
  1025.             while ((file = Readdir((zDIR *)G.wild_dir)) != NULL) {
  1026.                 Trace((stderr, "do_wild:  Readdir returns %s\n", file->d_name));
  1027.                 if (match(file->d_name, G.wildname, 1)) { /* 1 == ignore case */
  1028.                     Trace((stderr, "do_wild:  match() succeeds\n"));
  1029.                     if (G.have_dirname) {
  1030.                         strcpy(G.matchname, G.dirname);
  1031.                         strcpy(G.matchname+G.dirnamelen, file->d_name);
  1032.                     } else
  1033.                         strcpy(G.matchname, file->d_name);
  1034.                     return G.matchname;
  1035.                 }
  1036.             }
  1037.             /* if we get to here directory is exhausted, so close it */
  1038.             Closedir((zDIR *)G.wild_dir);
  1039.             G.wild_dir = NULL;
  1040.         }
  1041.         Trace((stderr, "do_wild:  Opendir(%s) returns NULL\n", G.dirname));
  1042.  
  1043.         /* return the raw wildspec in case that works (e.g., directory not
  1044.          * searchable, but filespec was not wild and file is readable) */
  1045.         strcpy(G.matchname, wildspec);
  1046.         return G.matchname;
  1047.     }
  1048.  
  1049.     /* last time through, might have failed opendir but returned raw wildspec */
  1050.     if (G.wild_dir == NULL) {
  1051.         G.notfirstcall = FALSE;    /* reset for new wildspec */
  1052.         if (G.have_dirname)
  1053.             free(G.dirname);
  1054.         return (char *)NULL;
  1055.     }
  1056.  
  1057.     /* If we've gotten this far, we've read and matched at least one entry
  1058.      * successfully (in a previous call), so dirname has been copied into
  1059.      * matchname already.
  1060.      */
  1061.     while ((file = Readdir((zDIR *)G.wild_dir)) != NULL)
  1062.         if (match(file->d_name, G.wildname, 1)) {   /* 1 == ignore case */
  1063.             if (G.have_dirname) {
  1064.                 /* strcpy(G.matchname, G.dirname); */
  1065.                 strcpy(G.matchname+G.dirnamelen, file->d_name);
  1066.             } else
  1067.                 strcpy(G.matchname, file->d_name);
  1068.             return G.matchname;
  1069.         }
  1070.  
  1071.     Closedir((zDIR *)G.wild_dir);  /* at least one entry read; nothing left */
  1072.     G.wild_dir = NULL;
  1073.     G.notfirstcall = FALSE;        /* reset for new wildspec */
  1074.     if (G.have_dirname)
  1075.         free(G.dirname);
  1076.     return (char *)NULL;
  1077.  
  1078. } /* end function do_wild() */
  1079.  
  1080. #endif /* !SFX */
  1081.  
  1082.  
  1083.  
  1084. /**********************/
  1085. /* Function mapattr() */
  1086. /**********************/
  1087.  
  1088. /* Identical to MS-DOS, OS/2 versions.  However, NT has a lot of extra
  1089.  * permission stuff, so this function should probably be extended in the
  1090.  * future. */
  1091.  
  1092. int mapattr(__G)
  1093.     __GDEF
  1094. {
  1095.     /* set archive bit (file is not backed up): */
  1096.     G.pInfo->file_attr = (unsigned)(G.crec.external_file_attributes | FILE_ATTRIBUTE_ARCHIVE) &
  1097.       0xff;
  1098.     return 0;
  1099.  
  1100. } /* end function mapattr() */
  1101.  
  1102.  
  1103.  
  1104.  
  1105. /************************/
  1106. /*  Function mapname()  */
  1107. /************************/
  1108.                              /* return 0 if no error, 1 if caution (filename */
  1109. int mapname(__G__ renamed)   /*  truncated), 2 if warning (skip file because */
  1110.     __GDEF                   /*  dir doesn't exist), 3 if error (skip file), */
  1111.     int renamed;             /*  or 10 if out of memory (skip file) */
  1112. {                            /*  [also IZ_VOL_LABEL, IZ_CREATED_DIR] */
  1113.     char pathcomp[FILNAMSIZ];   /* path-component buffer */
  1114.     char *pp, *cp=NULL;         /* character pointers */
  1115.     char *lastsemi = NULL;      /* pointer to last semi-colon in pathcomp */
  1116.     int error;
  1117.     register unsigned workch;   /* hold the character being tested */
  1118.  
  1119.  
  1120. /*---------------------------------------------------------------------------
  1121.     Initialize various pointers and counters and stuff.
  1122.   ---------------------------------------------------------------------------*/
  1123.  
  1124.     /* can create path as long as not just freshening, or if user told us */
  1125.     G.create_dirs = (!G.fflag || renamed);
  1126.  
  1127.     G.created_dir = FALSE;      /* not yet */
  1128.     G.renamed_fullpath = FALSE;
  1129.     G.fnlen = strlen(G.filename);
  1130.  
  1131.     if (renamed) {
  1132.         cp = G.filename - 1;    /* point to beginning of renamed name... */
  1133.         while (*++cp)
  1134.             if (*cp == '\\')    /* convert backslashes to forward */
  1135.                 *cp = '/';
  1136.         cp = G.filename;
  1137.         /* use temporary rootpath if user gave full pathname */
  1138.         if (G.filename[0] == '/') {
  1139.             G.renamed_fullpath = TRUE;
  1140.             pathcomp[0] = '/';  /* copy the '/' and terminate */
  1141.             pathcomp[1] = '\0';
  1142.             ++cp;
  1143.         } else if (isalpha(G.filename[0]) && G.filename[1] == ':') {
  1144.             G.renamed_fullpath = TRUE;
  1145.             pp = pathcomp;
  1146.             *pp++ = *cp++;      /* copy the "d:" (+ '/', possibly) */
  1147.             *pp++ = *cp++;
  1148.             if (*cp == '/')
  1149.                 *pp++ = *cp++;  /* otherwise add "./"? */
  1150.             *pp = '\0';
  1151.         }
  1152.     }
  1153.  
  1154.     /* pathcomp is ignored unless renamed_fullpath is TRUE: */
  1155.     if ((error = checkdir(__G__ pathcomp, INIT)) != 0)    /* init path buffer */
  1156.         return error;           /* ...unless no mem or vol label on hard disk */
  1157.  
  1158.     *pathcomp = '\0';           /* initialize translation buffer */
  1159.     pp = pathcomp;              /* point to translation buffer */
  1160.     if (!renamed) {             /* cp already set if renamed */
  1161.         if (G.jflag)            /* junking directories */
  1162.             cp = (char *)strrchr(G.filename, '/');
  1163.         if (cp == NULL)         /* no '/' or not junking dirs */
  1164.             cp = G.filename;    /* point to internal zipfile-member pathname */
  1165.         else
  1166.             ++cp;               /* point to start of last component of path */
  1167.     }
  1168.  
  1169. /*---------------------------------------------------------------------------
  1170.     Begin main loop through characters in filename.
  1171.   ---------------------------------------------------------------------------*/
  1172.  
  1173.     while ((workch = (uch)*cp++) != 0) {
  1174.  
  1175.         switch (workch) {
  1176.         case '/':             /* can assume -j flag not given */
  1177.             *pp = '\0';
  1178.             if ((error = checkdir(__G__ pathcomp, APPEND_DIR)) > 1)
  1179.                 return error;
  1180.             pp = pathcomp;    /* reset conversion buffer for next piece */
  1181.             lastsemi = NULL;  /* leave directory semi-colons alone */
  1182.             break;
  1183.  
  1184.         case ':':             /* drive names not stored in zipfile, */
  1185.         case '<':             /*  so no colons allowed */
  1186.         case '>':             /* no redirection symbols allowed either */
  1187.         case '|':             /* no pipe signs allowed */
  1188.         case '"':             /* no double quotes allowed */
  1189.         case '?':             /* no wildcards allowed */
  1190.         case '*':
  1191.             *pp++ = '_';      /* these rules apply equally to FAT and NTFS */
  1192.             break;
  1193.         case ';':             /* start of VMS version? */
  1194.             lastsemi = pp;    /* remove VMS version later... */
  1195.             *pp++ = ';';      /*  but keep semicolon for now */
  1196.             break;
  1197.  
  1198.         case ' ':             /* keep spaces unless specifically */
  1199.             /* NT cannot create filenames with spaces on FAT volumes */
  1200.             if (G.sflag || IsVolumeOldFAT(G.filename))
  1201.                 *pp++ = '_';
  1202.             else
  1203.                 *pp++ = ' ';
  1204.             break;
  1205.  
  1206.         default:
  1207.             /* allow European characters in filenames: */
  1208.             if (isprint(workch) || workch >= 127)
  1209.                 *pp++ = (char)workch;
  1210.         } /* end switch */
  1211.     } /* end while loop */
  1212.  
  1213.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  1214.  
  1215.     /* if not saving them, remove VMS version numbers (appended "###") */
  1216.     if (!G.V_flag && lastsemi) {
  1217.         pp = lastsemi + 1;        /* semi-colon was kept:  expect #'s after */
  1218.         while (isdigit((uch)(*pp)))
  1219.             ++pp;
  1220.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  1221.             *lastsemi = '\0';
  1222.     }
  1223.  
  1224. /*---------------------------------------------------------------------------
  1225.     Report if directory was created (and no file to create:  filename ended
  1226.     in '/'), check name to be sure it exists, and combine path and name be-
  1227.     fore exiting.
  1228.   ---------------------------------------------------------------------------*/
  1229.  
  1230.     if (G.filename[G.fnlen-1] == '/') {
  1231.         checkdir(__G__ G.filename, GETPATH);
  1232.         if (G.created_dir) {
  1233.             if (QCOND2) {
  1234.                 Info(slide, 0, ((char *)slide, "   creating: %-22s\n",
  1235.                   FnFilter1(G.filename)));
  1236.             }
  1237.             /* HG: are we setting the date & time on a newly created   */
  1238.             /*     dir?  Not quite sure how to do this.  It does not   */
  1239.             /*     seem to be done in the MS-DOS version of mapname(). */
  1240.  
  1241. #ifdef NTSD_EAS
  1242.             /* set extra fields, both stored-in-zipfile and .LONGNAME flavors */
  1243.             if (G.extra_field) { /* zipfile e.f. may have extended attribs */
  1244.                 int err = EvalExtraFields(__G__ G.filename, G.extra_field,
  1245.                                           G.lrec.extra_field_length);
  1246.  
  1247.                 if (err == IZ_EF_TRUNC) {
  1248.                     if (G.qflag)
  1249.                         Info(slide, 1, ((char *)slide, "%-22s ",
  1250.                           FnFilter1(G.filename)));
  1251.                     Info(slide, 1, ((char *)slide, LoadFarString(TruncNTSD),
  1252.                       makeword(G.extra_field+2)-10, G.qflag? "\n":""));
  1253.                 }
  1254.             }
  1255. #endif /* NTSD_EAS */
  1256.             return IZ_CREATED_DIR;      /* set dir time (note trailing '/') */
  1257.         }
  1258.         return 2;   /* dir existed already; don't look for data to extract */
  1259.     }
  1260.  
  1261.     if (*pathcomp == '\0') {
  1262.         Info(slide, 1, ((char *)slide, "mapname:  conversion of %s failed\n",
  1263.           FnFilter1(G.filename)));
  1264.         return 3;
  1265.     }
  1266.  
  1267.     checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
  1268.     checkdir(__G__ G.filename, GETPATH);
  1269.     Trace((stderr, "mapname returns with filename = [%s] (error = %d)\n\n",
  1270.       FnFilter1(G.filename), error));
  1271.  
  1272.     if (G.pInfo->vollabel) {    /* set the volume label now */
  1273.         char drive[4];
  1274. #ifdef __RSXNT__        /* RSXNT/EMX C rtl uses OEM charset */
  1275.         char *ansi_name = (char *)alloca(strlen(G.filename) + 1);
  1276.         INTERN_TO_ISO(G.filename, ansi_name);
  1277. #       define Ansi_Fname  ansi_name
  1278. #else
  1279. #       define Ansi_Fname  G.filename
  1280. #endif
  1281.  
  1282.         /* Build a drive string, e.g. "b:" */
  1283.         drive[0] = (char)('a' + G.nLabelDrive - 1);
  1284.         strcpy(drive + 1, ":\\");
  1285.         if (QCOND2)
  1286.             Info(slide, 0, ((char *)slide, "labelling %s %-22s\n", drive,
  1287.               FnFilter1(G.filename)));
  1288.         if (!SetVolumeLabel(drive, Ansi_Fname)) {
  1289.             Info(slide, 1, ((char *)slide,
  1290.               "mapname:  error setting volume label\n"));
  1291.             return 3;
  1292.         }
  1293.         return 2;   /* success:  skip the "extraction" quietly */
  1294. #undef Ansi_Fname
  1295.     }
  1296.  
  1297.     return error;
  1298.  
  1299. } /* end function mapname() */
  1300.  
  1301.  
  1302.  
  1303.  
  1304. /**********************/
  1305. /* Function map2fat() */        /* Not quite identical to OS/2 version */
  1306. /**********************/
  1307.  
  1308. static void map2fat(pathcomp, pEndFAT)
  1309.     char *pathcomp, **pEndFAT;
  1310. {
  1311.     char *ppc = pathcomp;       /* variable pointer to pathcomp */
  1312.     char *pEnd = *pEndFAT;      /* variable pointer to buildpathFAT */
  1313.     char *pBegin = *pEndFAT;    /* constant pointer to start of this comp. */
  1314.     char *last_dot = NULL;      /* last dot not converted to underscore */
  1315.     int dotname = FALSE;        /* flag:  path component begins with dot */
  1316.                                 /*  ("." and ".." don't count) */
  1317.     register unsigned workch;   /* hold the character being tested */
  1318.  
  1319.  
  1320.     /* Only need check those characters which are legal in NTFS but not
  1321.      * in FAT:  to get here, must already have passed through mapname.
  1322.      * Also must truncate path component to ensure 8.3 compliance.
  1323.      */
  1324.     while ((workch = (uch)*ppc++) != 0) {
  1325.         switch (workch) {
  1326.             case '[':
  1327.             case ']':
  1328.             case '+':
  1329.             case ',':
  1330.             case ';':
  1331.             case '=':
  1332.                 *pEnd++ = '_';      /* convert brackets to underscores */
  1333.                 break;
  1334.  
  1335.             case '.':
  1336.                 if (pEnd == *pEndFAT) {   /* nothing appended yet... */
  1337.                     if (*ppc == '\0')     /* don't bother appending a */
  1338.                         break;            /*  "./" component to the path */
  1339.                     else if (*ppc == '.' && ppc[1] == '\0') {   /* "../" */
  1340.                         *pEnd++ = '.';    /* add first dot, unchanged... */
  1341.                         ++ppc;            /* skip second dot, since it will */
  1342.                     } else {              /*  be "added" at end of if-block */
  1343.                         *pEnd++ = '_';    /* FAT doesn't allow null filename */
  1344.                         dotname = TRUE;   /*  bodies, so map .exrc -> _.exrc */
  1345.                     }                     /*  (extra '_' now, "dot" below) */
  1346.                 } else if (dotname) {     /* found a second dot, but still */
  1347.                     dotname = FALSE;      /*  have extra leading underscore: */
  1348.                     *pEnd = '\0';         /*  remove it by shifting chars */
  1349.                     pEnd = *pEndFAT + 1;  /*  left one space (e.g., .p1.p2: */
  1350.                     while (pEnd[1]) {     /*  __p1 -> _p1_p2 -> _p1.p2 when */
  1351.                         *pEnd = pEnd[1];  /*  finished) [opt.:  since first */
  1352.                         ++pEnd;           /*  two chars are same, can start */
  1353.                     }                     /*  shifting at second position] */
  1354.                 }
  1355.                 last_dot = pEnd;    /* point at last dot so far... */
  1356.                 *pEnd++ = '_';      /* convert dot to underscore for now */
  1357.                 break;
  1358.  
  1359.             default:
  1360.                 *pEnd++ = (char)workch;
  1361.  
  1362.         } /* end switch */
  1363.     } /* end while loop */
  1364.  
  1365.     *pEnd = '\0';                 /* terminate buildpathFAT */
  1366.  
  1367.     /* NOTE:  keep in mind that pEnd points to the end of the path
  1368.      * component, and *pEndFAT still points to the *beginning* of it...
  1369.      * Also note that the algorithm does not try to get too fancy:
  1370.      * if there are no dots already, the name either gets truncated
  1371.      * at 8 characters or the last underscore is converted to a dot
  1372.      * (only if more characters are saved that way).  In no case is
  1373.      * a dot inserted between existing characters.
  1374.      */
  1375.     if (last_dot == NULL) {       /* no dots:  check for underscores... */
  1376.         char *plu = strrchr(pBegin, '_');   /* pointer to last underscore */
  1377.  
  1378.         if (plu == NULL) {   /* no dots, no underscores:  truncate at 8 chars */
  1379.             *pEndFAT += 8;        /* (or could insert '.' and keep 11...?) */
  1380.             if (*pEndFAT > pEnd)
  1381.                 *pEndFAT = pEnd;  /* oops...didn't have 8 chars to truncate */
  1382.             else
  1383.                 **pEndFAT = '\0';
  1384.         } else if (MIN(plu - pBegin, 8) + MIN(pEnd - plu - 1, 3) > 8) {
  1385.             last_dot = plu;       /* be lazy:  drop through to next if-blk */
  1386.         } else if ((pEnd - *pEndFAT) > 8) {
  1387.             *pEndFAT += 8;        /* more fits into just basename than if */
  1388.             **pEndFAT = '\0';     /*  convert last underscore to dot */
  1389.         } else
  1390.             *pEndFAT = pEnd;      /* whole thing fits into 8 chars or less */
  1391.     }
  1392.  
  1393.     if (last_dot != NULL) {       /* one dot (or two, in the case of */
  1394.         *last_dot = '.';          /*  "..") is OK:  put it back in */
  1395.  
  1396.         if ((last_dot - pBegin) > 8) {
  1397.             char *p=last_dot, *q=pBegin+8;
  1398.             int i;
  1399.  
  1400.             for (i = 0;  (i < 4) && *p;  ++i)  /* too many chars in basename: */
  1401.                 *q++ = *p++;                   /*  shift .ext left and trun- */
  1402.             *q = '\0';                         /*  cate/terminate it */
  1403.             *pEndFAT = q;
  1404.         } else if ((pEnd - last_dot) > 4) {    /* too many chars in extension */
  1405.             *pEndFAT = last_dot + 4;
  1406.             **pEndFAT = '\0';
  1407.         } else
  1408.             *pEndFAT = pEnd;   /* filename is fine; point at terminating zero */
  1409.     }
  1410. } /* end function map2fat() */
  1411.  
  1412.  
  1413.  
  1414.  
  1415. /***********************/       /* Borrowed from os2.c for UnZip 5.1.        */
  1416. /* Function checkdir() */       /* Difference: no EA stuff                   */
  1417. /***********************/       /*             HPFS stuff works on NTFS too  */
  1418.  
  1419. int checkdir(__G__ pathcomp, flag)
  1420.     __GDEF
  1421.     char *pathcomp;
  1422.     int flag;
  1423. /*
  1424.  * returns:  1 - (on APPEND_NAME) truncated filename
  1425.  *           2 - path doesn't exist, not allowed to create
  1426.  *           3 - path doesn't exist, tried to create and failed; or
  1427.  *               path exists and is not a directory, but is supposed to be
  1428.  *           4 - path is too long
  1429.  *          10 - can't allocate memory for filename buffers
  1430.  */
  1431. {
  1432.  /* static int rootlen = 0;     */   /* length of rootpath */
  1433.  /* static char *rootpath;      */   /* user's "extract-to" directory */
  1434.  /* static char *buildpathHPFS; */   /* full path (so far) to extracted file, */
  1435.  /* static char *buildpathFAT;  */   /*  both HPFS/EA (main) and FAT versions */
  1436.  /* static char *endHPFS;       */   /* corresponding pointers to end of */
  1437.  /* static char *endFAT;        */   /*  buildpath ('\0') */
  1438.  
  1439. #   define FN_MASK   7
  1440. #   define FUNCTION  (flag & FN_MASK)
  1441.  
  1442.  
  1443.  
  1444. /*---------------------------------------------------------------------------
  1445.     APPEND_DIR:  append the path component to the path being built and check
  1446.     for its existence.  If doesn't exist and we are creating directories, do
  1447.     so for this one; else signal success or error as appropriate.
  1448.   ---------------------------------------------------------------------------*/
  1449.  
  1450.     if (FUNCTION == APPEND_DIR) {
  1451.         char *p = pathcomp;
  1452.         int too_long=FALSE;
  1453.  
  1454.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  1455.         while ((*G.endHPFS = *p++) != '\0')     /* copy to HPFS filename */
  1456.             ++G.endHPFS;
  1457.         if (IsFileNameValid(G.buildpathHPFS)) {
  1458.             p = pathcomp;
  1459.             while ((*G.endFAT = *p++) != '\0')  /* copy to FAT filename, too */
  1460.                 ++G.endFAT;
  1461.         } else
  1462.             map2fat(pathcomp, &G.endFAT);   /* map into FAT fn, update endFAT */
  1463.  
  1464.         /* GRR:  could do better check, see if overrunning buffer as we go:
  1465.          * check endHPFS-buildpathHPFS after each append, set warning variable
  1466.          * if within 20 of FILNAMSIZ; then if var set, do careful check when
  1467.          * appending.  Clear variable when begin new path. */
  1468.  
  1469.         /* next check:  need to append '/', at least one-char name, '\0' */
  1470.         if ((G.endHPFS-G.buildpathHPFS) > FILNAMSIZ-3)
  1471.             too_long = TRUE;                    /* check if extracting dir? */
  1472. #ifdef FIX_STAT_BUG
  1473.         /* Borland C++ 5.0 does not handle a call to stat() well if the
  1474.          * directory does not exist (it tends to crash in strange places.)
  1475.          * This is apparently a problem only when compiling for GUI rather
  1476.          * than console. The code below attempts to work around this problem.
  1477.          */
  1478.         if (access(G.buildpathFAT, 0) != 0) {
  1479.             if (!G.create_dirs) { /* told not to create (freshening) */
  1480.                 free(G.buildpathHPFS);
  1481.                 free(G.buildpathFAT);
  1482.                 return 2;         /* path doesn't exist:  nothing to do */
  1483.             }
  1484.             if (too_long) {   /* GRR:  should allow FAT extraction w/o EAs */
  1485.                 Info(slide, 1, ((char *)slide,
  1486.                   "checkdir error:  path too long: %s\n",
  1487.                   FnFilter1(G.buildpathHPFS)));
  1488.                 free(G.buildpathHPFS);
  1489.                 free(G.buildpathFAT);
  1490.                 return 4;         /* no room for filenames:  fatal */
  1491.             }
  1492.             if (MKDIR(G.buildpathFAT, 0777) == -1) { /* create the directory */
  1493.                 Info(slide, 1, ((char *)slide,
  1494.                   "checkdir error:  can't create %s\n\
  1495.                  unable to process %s.\n",
  1496.                   FnFilter2(G.buildpathFAT), FnFilter1(G.filename)));
  1497.                 free(G.buildpathHPFS);
  1498.                 free(G.buildpathFAT);
  1499.                 return 3;      /* path didn't exist, tried to create, failed */
  1500.             }
  1501.             G.created_dir = TRUE;
  1502.         }
  1503. #endif /* FIX_STAT_BUG */
  1504.         if (SSTAT(G.buildpathFAT, &G.statbuf))   /* path doesn't exist */
  1505.         {
  1506.             if (!G.create_dirs) { /* told not to create (freshening) */
  1507.                 free(G.buildpathHPFS);
  1508.                 free(G.buildpathFAT);
  1509.                 return 2;         /* path doesn't exist:  nothing to do */
  1510.             }
  1511.             if (too_long) {   /* GRR:  should allow FAT extraction w/o EAs */
  1512.                 Info(slide, 1, ((char *)slide,
  1513.                   "checkdir error:  path too long: %s\n",
  1514.                   FnFilter1(G.buildpathHPFS)));
  1515.                 free(G.buildpathHPFS);
  1516.                 free(G.buildpathFAT);
  1517.                 return 4;         /* no room for filenames:  fatal */
  1518.             }
  1519.             if (MKDIR(G.buildpathFAT, 0777) == -1) { /* create the directory */
  1520.                 Info(slide, 1, ((char *)slide,
  1521.                   "checkdir error:  can't create %s\n\
  1522.                  unable to process %s.\n",
  1523.                   FnFilter2(G.buildpathFAT), FnFilter1(G.filename)));
  1524.                 free(G.buildpathHPFS);
  1525.                 free(G.buildpathFAT);
  1526.                 return 3;      /* path didn't exist, tried to create, failed */
  1527.             }
  1528.             G.created_dir = TRUE;
  1529.         } else if (!S_ISDIR(G.statbuf.st_mode)) {
  1530.             Info(slide, 1, ((char *)slide,
  1531.               "checkdir error:  %s exists but is not directory\n   \
  1532.               unable to process %s.\n",
  1533.               FnFilter2(G.buildpathFAT), FnFilter1(G.filename)));
  1534.             free(G.buildpathHPFS);
  1535.             free(G.buildpathFAT);
  1536.             return 3;          /* path existed but wasn't dir */
  1537.         }
  1538.         if (too_long) {
  1539.             Info(slide, 1, ((char *)slide,
  1540.               "checkdir error:  path too long: %s\n",
  1541.                FnFilter1(G.buildpathHPFS)));
  1542.             free(G.buildpathHPFS);
  1543.             free(G.buildpathFAT);
  1544.             return 4;         /* no room for filenames:  fatal */
  1545.         }
  1546.         *G.endHPFS++ = '/';
  1547.         *G.endFAT++ = '/';
  1548.         *G.endHPFS = *G.endFAT = '\0';
  1549.         Trace((stderr, "buildpathHPFS now = [%s]\nbuildpathFAT now =  [%s]\n",
  1550.           FnFilter1(G.buildpathHPFS), FnFilter2(G.buildpathFAT)));
  1551.         return 0;
  1552.  
  1553.     } /* end if (FUNCTION == APPEND_DIR) */
  1554.  
  1555. /*---------------------------------------------------------------------------
  1556.     GETPATH:  copy full FAT path to the string pointed at by pathcomp (want
  1557.     filename to reflect name used on disk, not EAs; if full path is HPFS,
  1558.     buildpathFAT and buildpathHPFS will be identical).  Also free both paths.
  1559.   ---------------------------------------------------------------------------*/
  1560.  
  1561.     if (FUNCTION == GETPATH) {
  1562.         Trace((stderr, "getting and freeing FAT path [%s]\n",
  1563.           FnFilter1(G.buildpathFAT)));
  1564.         Trace((stderr, "freeing HPFS path [%s]\n",
  1565.           FnFilter1(G.buildpathHPFS)));
  1566.         strcpy(pathcomp, G.buildpathFAT);
  1567.         free(G.buildpathFAT);
  1568.         free(G.buildpathHPFS);
  1569.         G.buildpathHPFS = G.buildpathFAT = G.endHPFS = G.endFAT = NULL;
  1570.         return 0;
  1571.     }
  1572.  
  1573. /*---------------------------------------------------------------------------
  1574.     APPEND_NAME:  assume the path component is the filename; append it and
  1575.     return without checking for existence.
  1576.   ---------------------------------------------------------------------------*/
  1577.  
  1578.     if (FUNCTION == APPEND_NAME) {
  1579.         char *p = pathcomp;
  1580.         int error = 0;
  1581.  
  1582.         Trace((stderr, "appending filename [%s]\n", FnFilter1(pathcomp)));
  1583.         while ((*G.endHPFS = *p++) != '\0') {   /* copy to HPFS filename */
  1584.             ++G.endHPFS;
  1585.             if ((G.endHPFS-G.buildpathHPFS) >= FILNAMSIZ) {
  1586.                 *--G.endHPFS = '\0';
  1587.                 Info(slide, 1, ((char *)slide,
  1588.                   "checkdir warning:  path too long; truncating\n \
  1589.                   %s\n                -> %s\n",
  1590.                   FnFilter1(G.filename), FnFilter2(G.buildpathHPFS)));
  1591.                 error = 1;   /* filename truncated */
  1592.             }
  1593.         }
  1594.  
  1595.         if ( G.pInfo->vollabel || IsFileNameValid(G.buildpathHPFS)) {
  1596.             p = pathcomp;
  1597.             while ((*G.endFAT = *p++) != '\0')  /* copy to FAT filename, too */
  1598.                 ++G.endFAT;
  1599.         } else
  1600.             map2fat(pathcomp, &G.endFAT);   /* map into FAT fn, update endFAT */
  1601.         Trace((stderr, "buildpathHPFS: %s\nbuildpathFAT:  %s\n",
  1602.           FnFilter1(G.buildpathHPFS), FnFilter2(G.buildpathFAT)));
  1603.  
  1604.         return error;  /* could check for existence, prompt for new name... */
  1605.  
  1606.     } /* end if (FUNCTION == APPEND_NAME) */
  1607.  
  1608. /*---------------------------------------------------------------------------
  1609.     INIT:  allocate and initialize buffer space for the file currently being
  1610.     extracted.  If file was renamed with an absolute path, don't prepend the
  1611.     extract-to path.
  1612.   ---------------------------------------------------------------------------*/
  1613.  
  1614.     if (FUNCTION == INIT) {
  1615.         Trace((stderr, "initializing buildpathHPFS and buildpathFAT to "));
  1616.         if ((G.buildpathHPFS = (char *)malloc(G.fnlen+G.rootlen+1)) == NULL)
  1617.             return 10;
  1618.         if ((G.buildpathFAT = (char *)malloc(G.fnlen+G.rootlen+1)) == NULL) {
  1619.             free(G.buildpathHPFS);
  1620.             return 10;
  1621.         }
  1622.         if (G.pInfo->vollabel) { /* use root or renamed path, but don't store */
  1623. /* GRR:  for network drives, do strchr() and return IZ_VOL_LABEL if not [1] */
  1624.             if (G.renamed_fullpath && pathcomp[1] == ':')
  1625.                 *G.buildpathHPFS = (char)ToLower(*pathcomp);
  1626.             else if (!G.renamed_fullpath && G.rootlen > 1 &&
  1627.                      G.rootpath[1] == ':')
  1628.                 *G.buildpathHPFS = (char)ToLower(*G.rootpath);
  1629.             else {
  1630.                 char tmpN[MAX_PATH], *tmpP;
  1631.                 if (GetFullPathName(".", MAX_PATH, tmpN, &tmpP) > MAX_PATH)
  1632.                 { /* by definition of MAX_PATH we should never get here */
  1633.                     Info(slide, 1, ((char *)slide,
  1634.                       "checkdir warning: current dir path too long\n"));
  1635.                     return 1;   /* can't get drive letter */
  1636.                 }
  1637.                 G.nLabelDrive = *tmpN - 'a' + 1;
  1638.                 *G.buildpathHPFS = (char)(G.nLabelDrive - 1 + 'a');
  1639.             }
  1640.             G.nLabelDrive = *G.buildpathHPFS - 'a' + 1; /* save for mapname() */
  1641.             if (G.volflag == 0 || *G.buildpathHPFS < 'a'  /* no labels/bogus? */
  1642.                  || (G.volflag == 1 && !isfloppy(G.nLabelDrive))) { /* !fixed */
  1643.                 free(G.buildpathHPFS);
  1644.                 free(G.buildpathFAT);
  1645.                 return IZ_VOL_LABEL;   /* skipping with message */
  1646.             }
  1647.             *G.buildpathHPFS = '\0';
  1648.         } else if (G.renamed_fullpath) /* pathcomp = valid data */
  1649.             strcpy(G.buildpathHPFS, pathcomp);
  1650.         else if (G.rootlen > 0)
  1651.             strcpy(G.buildpathHPFS, G.rootpath);
  1652.         else
  1653.             *G.buildpathHPFS = '\0';
  1654.         G.endHPFS = G.buildpathHPFS;
  1655.         G.endFAT = G.buildpathFAT;
  1656.         while ((*G.endFAT = *G.endHPFS) != '\0') {
  1657.             ++G.endFAT;
  1658.             ++G.endHPFS;
  1659.         }
  1660.         Trace((stderr, "[%s]\n", FnFilter1(G.buildpathHPFS)));
  1661.         return 0;
  1662.     }
  1663.  
  1664. /*---------------------------------------------------------------------------
  1665.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  1666.     sary; else assume it's a zipfile member and return.  This path segment
  1667.     gets used in extracting all members from every zipfile specified on the
  1668.     command line.  Note that under OS/2 and MS-DOS, if a candidate extract-to
  1669.     directory specification includes a drive letter (leading "x:"), it is
  1670.     treated just as if it had a trailing '/'--that is, one directory level
  1671.     will be created if the path doesn't exist, unless this is otherwise pro-
  1672.     hibited (e.g., freshening).
  1673.   ---------------------------------------------------------------------------*/
  1674.  
  1675. #if (!defined(SFX) || defined(SFX_EXDIR))
  1676.     if (FUNCTION == ROOT) {
  1677.         Trace((stderr, "initializing root path to [%s]\n",
  1678.           FnFilter1(pathcomp)));
  1679.         if (pathcomp == NULL) {
  1680.             G.rootlen = 0;
  1681.             return 0;
  1682.         }
  1683.         if ((G.rootlen = strlen(pathcomp)) > 0) {
  1684.             int had_trailing_pathsep=FALSE, has_drive=FALSE, xtra=2;
  1685.  
  1686.             if (isalpha(pathcomp[0]) && pathcomp[1] == ':')
  1687.                 has_drive = TRUE;   /* drive designator */
  1688.             if (pathcomp[G.rootlen-1] == '/' || pathcomp[G.rootlen-1] == '\\') {
  1689.                 pathcomp[--G.rootlen] = '\0';
  1690.                 had_trailing_pathsep = TRUE;
  1691.             }
  1692.             if (has_drive && (G.rootlen == 2)) {
  1693.                 if (!had_trailing_pathsep)   /* i.e., original wasn't "x:/" */
  1694.                     xtra = 3;      /* room for '.' + '/' + 0 at end of "x:" */
  1695.             } else if (G.rootlen > 0) {   /* need not check "x:." and "x:/" */
  1696.                 if (SSTAT(pathcomp, &G.statbuf) || !S_ISDIR(G.statbuf.st_mode))
  1697.                 {
  1698.                     /* path does not exist */
  1699.                     if (!G.create_dirs /* || iswild(pathcomp) */ ) {
  1700.                         G.rootlen = 0;
  1701.                         return 2;   /* treat as stored file */
  1702.                     }
  1703.                     /* create directory (could add loop here to scan pathcomp
  1704.                      * and create more than one level, but really necessary?) */
  1705.                     if (MKDIR(pathcomp, 0777) == -1) {
  1706.                         Info(slide, 1, ((char *)slide,
  1707.                           "checkdir:  can't create extraction directory: %s\n",
  1708.                           FnFilter1(pathcomp)));
  1709.                         G.rootlen = 0; /* path didn't exist, tried to create, */
  1710.                         return 3;  /* failed:  file exists, or need 2+ levels */
  1711.                     }
  1712.                 }
  1713.             }
  1714.             if ((G.rootpath = (char *)malloc(G.rootlen+xtra)) == NULL) {
  1715.                 G.rootlen = 0;
  1716.                 return 10;
  1717.             }
  1718.             strcpy(G.rootpath, pathcomp);
  1719.             if (xtra == 3)                  /* had just "x:", make "x:." */
  1720.                 G.rootpath[G.rootlen++] = '.';
  1721.             G.rootpath[G.rootlen++] = '/';
  1722.             G.rootpath[G.rootlen] = '\0';
  1723.             Trace((stderr, "rootpath now = [%s]\n", FnFilter1(G.rootpath)));
  1724.         }
  1725.         return 0;
  1726.     }
  1727. #endif /* !SFX || SFX_EXDIR */
  1728.  
  1729. /*---------------------------------------------------------------------------
  1730.     END:  free rootpath, immediately prior to program exit.
  1731.   ---------------------------------------------------------------------------*/
  1732.  
  1733.     if (FUNCTION == END) {
  1734.         Trace((stderr, "freeing rootpath\n"));
  1735.         if (G.rootlen > 0)
  1736.             free(G.rootpath);
  1737.         return 0;
  1738.     }
  1739.  
  1740.     return 99;  /* should never reach */
  1741.  
  1742. } /* end function checkdir() */
  1743.  
  1744.  
  1745.  
  1746.  
  1747.  
  1748. #ifndef SFX
  1749. #ifndef WINDLL
  1750.  
  1751. /************************/
  1752. /*  Function version()  */
  1753. /************************/
  1754.  
  1755. void version(__G)
  1756.     __GDEF
  1757. {
  1758.     int len;
  1759. #if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__DJGPP__))
  1760.     char buf[80];
  1761. #if (defined(_MSC_VER) && (_MSC_VER > 900))
  1762.     char buf2[80];
  1763. #endif
  1764. #endif
  1765.  
  1766.     len = sprintf((char *)slide, CompiledWith,
  1767.  
  1768. #ifdef _MSC_VER  /* MSC == VC++, but what about SDK compiler? */
  1769.       (sprintf(buf, "Microsoft C %d.%02d ", _MSC_VER/100, _MSC_VER%100), buf),
  1770. #  if (_MSC_VER == 800)
  1771.       "(Visual C++ v1.1)",
  1772. #  elif (_MSC_VER == 850)
  1773.       "(Windows NT v3.5 SDK)",
  1774. #  elif (_MSC_VER == 900)
  1775.       "(Visual C++ v2.x)",
  1776. #  elif (_MSC_VER > 900)
  1777.       (sprintf(buf2, "(Visual C++ %d.%d)", _MSC_VER/100 - 6, _MSC_VER%100/10),
  1778.         buf2),
  1779. #  else
  1780.       "(bad version)",
  1781. #  endif
  1782. #elif defined(__WATCOMC__)
  1783. #  if (__WATCOMC__ % 10 > 0)
  1784.       (sprintf(buf, "Watcom C/C++ %d.%02d", __WATCOMC__ / 100,
  1785.        __WATCOMC__ % 100), buf), "",
  1786. #  else
  1787.       (sprintf(buf, "Watcom C/C++ %d.%d", __WATCOMC__ / 100,
  1788.        (__WATCOMC__ % 100) / 10), buf), "",
  1789. #  endif
  1790. #elif defined(__BORLANDC__)
  1791.       "Borland C++",
  1792. #  if (__BORLANDC__ < 0x0200)
  1793.       " 1.0",
  1794. #  elif (__BORLANDC__ == 0x0200)
  1795.       " 2.0",
  1796. #  elif (__BORLANDC__ == 0x0400)
  1797.       " 3.0",
  1798. #  elif (__BORLANDC__ == 0x0410)   /* __BCPLUSPLUS__ = 0x0310 */
  1799.       " 3.1",
  1800. #  elif (__BORLANDC__ == 0x0452)   /* __BCPLUSPLUS__ = 0x0320 */
  1801.       " 4.0 or 4.02",
  1802. #  elif (__BORLANDC__ == 0x0460)   /* __BCPLUSPLUS__ = 0x0340 */
  1803.       " 4.5",
  1804. #  elif (__BORLANDC__ == 0x0500)   /* GRR:  assume this will stay sync'd? */
  1805.       " 5.0",
  1806. #  else
  1807.       " later than 5.0",
  1808. #  endif
  1809. #elif defined(__GNUC__)
  1810. #  ifdef __RSXNT__
  1811. #    if defined(__EMX__)
  1812.       "rsxnt(emx)+gcc ",
  1813. #    elif defined(__DJGPP__)
  1814.       (sprintf(buf, "rsxnt(djgpp) v%d.%02d / gcc ", __DJGPP__, __DJGPP_MINOR__),
  1815.        buf),
  1816. #    elif defined(__GO32__)
  1817.       "rsxnt(djgpp) v1.x / gcc ",
  1818. #    else
  1819.       "rsxnt(unknown) / gcc ",
  1820. #    endif
  1821. #  else
  1822.       "gcc ",
  1823. #  endif
  1824.       __VERSION__,
  1825. #else /* !_MSC_VER, !__WATCOMC__, !__BORLANDC__, !__GNUC__ */
  1826.       "unknown compiler (SDK?)", "",
  1827. #endif /* ?compilers */
  1828.  
  1829.       "Windows 95 / Windows NT", "\n(32-bit)",
  1830.  
  1831. #ifdef __DATE__
  1832.       " on ", __DATE__
  1833. #else
  1834.       "", ""
  1835. #endif
  1836.     );
  1837.  
  1838.     (*G.message)((zvoid *)&G, slide, (ulg)len, 0);
  1839.  
  1840.     return;
  1841.  
  1842. } /* end function version() */
  1843.  
  1844. #endif /* !WINDLL */
  1845. #endif /* !SFX */
  1846.  
  1847.  
  1848.  
  1849.  
  1850. #ifdef __WATCOMC__
  1851.  
  1852. /* This papers over a bug in Watcom 10.6's standard library... sigh */
  1853. /* Apparently it applies to both the DOS and Win32 stat()s.         */
  1854. /* I believe they said this was fixed for 11.0?                     */
  1855.  
  1856. int stat_bandaid(const char *path, struct stat *buf)
  1857. {
  1858.   char newname[4];
  1859.   if (!stat(path, buf))
  1860.     return 0;
  1861.   else if (!strcmp(path, ".") || (path[0] && !strcmp(path + 1, ":."))) {
  1862.     strcpy(newname, path);
  1863.     newname[strlen(path) - 1] = '\\';   /* stat(".") fails for root! */
  1864.     return stat(newname, buf);
  1865.   } else
  1866.     return -1;
  1867. }
  1868.  
  1869. /* Watcom 10.6's getch() does not handle Alt+<digit><digit><digit>. */
  1870. /* Note that if PASSWD_FROM_STDIN is defined, the file containing   */
  1871. /* the password must have a carriage return after the word, not a   */
  1872. /* Unix-style newline (linefeed only).  This discards linefeeds.    */
  1873.  
  1874. int getch(void)
  1875. {
  1876.   HANDLE stin;
  1877.   DWORD rc;
  1878.   unsigned char buf[2];
  1879.   int ret = -1;
  1880.  
  1881. #  ifdef PASSWD_FROM_STDIN
  1882.   DWORD odemode = ~0;
  1883.   stin = GetStdHandle(STD_INPUT_HANDLE);
  1884.   if (GetConsoleMode(stin, &odemode))
  1885.     SetConsoleMode(stin, ENABLE_PROCESSED_INPUT);  /* raw except ^C noticed */
  1886. #  else
  1887.   if (!(stin = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
  1888.                           FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)))
  1889.     return -1;
  1890.   SetConsoleMode(stin, ENABLE_PROCESSED_INPUT);    /* raw except ^C noticed */
  1891. #  endif
  1892.   if (ReadFile(stin, &buf, 1, &rc, NULL) && rc == 1)
  1893.     ret = buf[0];
  1894.   /* when the user hits return we get CR LF.  We discard the LF, not the CR,
  1895.    * because when we call this for the first time after a previous input
  1896.    * such as the one for "replace foo? [y]es, ..." the LF may still be in
  1897.    * the input stream before whatever the user types at our prompt. */
  1898.   if (ret == '\n')
  1899.     if (ReadFile(stin, &buf, 1, &rc, NULL) && rc == 1)
  1900.       ret = buf[0];
  1901. #  ifdef PASSWD_FROM_STDIN
  1902.   if (odemode != ~0)
  1903.     SetConsoleMode(stin, odemode);
  1904. #  else
  1905.   CloseHandle(stin);
  1906. #  endif
  1907.   return ret;
  1908. }
  1909.  
  1910. #endif /* __WATCOMC__ */
  1911.